package com.zkt.subject.infra.basic.service.impl;

import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zkt.subject.infra.basic.entity.SubjectUserRecord;
import com.zkt.subject.infra.basic.mapper.SubjectUserRecordMapper;
import com.zkt.subject.infra.basic.service.ISubjectUserRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 用户做题记录表 服务实现类
 * </p>
 *
 * @author 赵开泰
 * @since 2025-03-27
 */
@Service
public class SubjectUserRecordServiceImpl extends ServiceImpl<SubjectUserRecordMapper, SubjectUserRecord> implements ISubjectUserRecordService {
	
	@Resource
	private SubjectUserRecordMapper subjectUserRecordMapper;
	
	/**
	 * 根据条件统计总数
	 *
	 * @param subjectUserRecord 条件
	 * @return 总数
	 */
	@Override
	public Long count(SubjectUserRecord subjectUserRecord) {
		return subjectUserRecordMapper.count(subjectUserRecord);
	}
	
	/**
	 * 根据条件查询用户做题记录
	 *
	 * @param subjectUserRecord 条件
	 * @param start           第几页
	 * @param pageSize          查询条数
	 * @return 用户做题记录
	 */
	@Override
	public List<SubjectUserRecord> queryPage(SubjectUserRecord subjectUserRecord, int start, int pageSize) {
		return subjectUserRecordMapper.queryPage(subjectUserRecord, start , pageSize);
	}
	
	/**
	 * 根据条件查询用户所有的做题记录
	 *
	 * @param subjectUserRecord 条件
	 * @return 查询结果
	 */
	@Override
	public List<SubjectUserRecord> query(SubjectUserRecord subjectUserRecord) {
		return lambdaQuery()
				.eq(ObjUtil.isNotNull(subjectUserRecord.getUserId()), SubjectUserRecord::getUserId, subjectUserRecord.getUserId())
				.eq(ObjUtil.isNotNull(subjectUserRecord.getSubjectId()), SubjectUserRecord::getSubjectId, subjectUserRecord.getSubjectId())
				.like(StrUtil.isNotBlank(subjectUserRecord.getSubjectName()), SubjectUserRecord::getSubjectName,
						subjectUserRecord.getSubjectName())
				.eq(ObjUtil.isNotNull(subjectUserRecord.getSubjectDifficult()), SubjectUserRecord::getSubjectDifficult,
						subjectUserRecord.getSubjectDifficult())
				.eq(ObjUtil.isNotNull(subjectUserRecord.getLabelId()), SubjectUserRecord::getLabelId, subjectUserRecord.getLabelId())
				.orderByDesc(SubjectUserRecord::getCreatedTime)
				.list();
	}
}
